package com.security.antitheft.module;


import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.provider.MediaStore;

public class PicsObserver extends ContentObserver {

	private Context context;

	public PicsObserver(Context c) {
		super(null);
		this.context = c;
	}

	@Override
	public void onChange(boolean selfChange) {
		super.onChange(selfChange);

		try {
			if (!ShareSetting.isEnablePics(context)) return;

			long lasteId = ShareSetting.lastPics(context);

			Cursor cursor = context.getContentResolver().query(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Images.ImageColumns._ID + " DESC");
			int count = 0;
			boolean isset = false;
			long newlastid = lasteId;
			while (cursor.moveToNext()) {
				
				long id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns._ID));
				if (id > lasteId) {
					if (!isset) {
						newlastid = id;
						isset = true;
					}

					long timestamp = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATE_TAKEN));
					String filepath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
					//save to queue
					QueueHelper queue = new QueueHelper(context, true);
					queue.addPics(timestamp, filepath);
					queue.close();
					queue = null;
					count++;
					if (count > 5) break;

				} else {
					break;
				}
			}
			cursor.close();
			ShareSetting.setLastPics(context, newlastid);
		} catch (Exception e) {
		}
	}


	@Override
	public boolean deliverSelfNotifications() {
		return false;
	}
}